package com.ruoyi.web.controller.fs;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.service.IFsCaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsCaseCourtContact;
import com.ruoyi.fs.service.IFsCaseCourtContactService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案件法院沟通Controller
 *
 * @author hero
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/fs/contact")
public class FsCaseCourtContactController extends BaseController {
    @Autowired
    private IFsCaseCourtContactService fsCaseCourtContactService;
    @Autowired
    private IFsCaseService fsCaseService;

    /**
     * 查询案件法院沟通列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCaseCourtContact fsCaseCourtContact) {
        startPage();
        List<FsCaseCourtContact> list = fsCaseCourtContactService.selectFsCaseCourtContactList(fsCaseCourtContact);
        return getDataTable(list);
    }

    /**
     * 导出案件法院沟通列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:export')")
    @Log(title = "案件法院沟通", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsCaseCourtContact fsCaseCourtContact) {
        List<FsCaseCourtContact> list = fsCaseCourtContactService.selectFsCaseCourtContactList(fsCaseCourtContact);
        ExcelUtil<FsCaseCourtContact> util = new ExcelUtil<FsCaseCourtContact>(FsCaseCourtContact.class);
        util.exportExcel(response, list, "案件法院沟通数据");
    }

    /**
     * 获取案件法院沟通详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsCaseCourtContactService.selectFsCaseCourtContactById(id));
    }

    /**
     * 新增案件法院沟通
     */
    @PreAuthorize("@ss.hasPermi('fs:case:add')")
    @Log(title = "案件法院沟通", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCaseCourtContact fsCaseCourtContact) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        fsCaseCourtContact.setCjr(user.getUserId());
        fsCaseCourtContact.setCreateBy(user.getNickName());
        FsCase caseVo = fsCaseService.selectFsCaseById(fsCaseCourtContact.getCaseId());
        if (fsCaseCourtContact!=null && fsCaseCourtContact.getIfTb()) {
            FsCase query = new FsCase();
            query.setBatchId(caseVo.getBatchId());
            query.setFyid(caseVo.getFyid());
            List<FsCase> cases = fsCaseService.selectFsCaseList(query);
            for (int i = 0; i < cases.size(); i++) {
                FsCase fsCase=cases.get(i);
                fsCaseCourtContact.setCaseId(fsCase.getId());
                fsCaseCourtContactService.insertFsCaseCourtContact(fsCaseCourtContact);
                fsCase.setIfCourtContact(1);
                fsCaseService.updateFsCase(fsCase);
            }
        } else {
            caseVo.setIfCourtContact(1);
            fsCaseService.updateFsCase(caseVo);
            fsCaseCourtContactService.insertFsCaseCourtContact(fsCaseCourtContact);
        }
        return success();
    }

    /**
     * 修改案件法院沟通
     */
    @PreAuthorize("@ss.hasPermi('fs:case:edit')")
    @Log(title = "案件法院沟通", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCaseCourtContact fsCaseCourtContact) {
        return toAjax(fsCaseCourtContactService.updateFsCaseCourtContact(fsCaseCourtContact));
    }

    /**
     * 删除案件法院沟通
     */
    @PreAuthorize("@ss.hasPermi('fs:case:remove')")
    @Log(title = "案件法院沟通", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsCaseCourtContactService.deleteFsCaseCourtContactByIds(ids));
    }
}
